package com.funcablaze.tpxterm.func.terminal;

import com.techsenger.jeditermfx.core.TerminalColor;
import com.techsenger.jeditermfx.core.TextStyle;
import com.techsenger.jeditermfx.ui.settings.DefaultSettingsProvider;
import javafx.scene.text.Font;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.List;

public class Theme extends DefaultSettingsProvider {
	
	private final Config config;
	
	public Theme(Config config) {
		this.config = config;
	}
	
	@Override
	public @NotNull TerminalColor getDefaultBackground() {
		return config.backgroundColor;
	}
	
	@Override
	public @NotNull TerminalColor getDefaultForeground() {
		return config.textColor;
	}
	
	@Override
	public @NotNull TextStyle getFoundPatternColor() {
		return new TextStyle(config.foundTextColor, config.foundBackgroundColor);
	}
	
	@Override
	public @NotNull TextStyle getSelectionColor() {
		return new TextStyle(config.selectTextColor, config.selectBackColor);
	}
	
	@Override
	public TextStyle getHyperlinkColor() {
		return new TextStyle(config.linkTextColor, config.linkBackColor);
	}
	
	@Override
	public float getTerminalFontSize() {
		return config.fontSize;
	}
	
	@Override
	public @Nullable Font getTerminalFont() {
		return Font.font(config.fontFamily, config.fontSize);
	}
	
	@Override
	public float getLineSpacing() {
		return 1.3f;
	}
	
	@Override
	public @NotNull TextStyle getDefaultStyle() {
		return new TextStyle(config.textColor, config.backgroundColor);
	}
	
	@Override
	public boolean useInverseSelectionColor() {
		return config.useInverseSelection;
	}
	
	@Override
	public boolean copyOnSelect() {
		return config.copyOnSelect;
	}
	
	@Override
	public boolean pasteOnMiddleMouseClick() {
		return config.pasteOnMiddleClick;
	}
	
	@Override
	public int getBufferMaxLinesCount() {
		return config.maxScrollbarLines;
	}
	
	@Override
	public boolean audibleBell() {
		return config.audibleBell;
	}
	
	@Override
	public boolean enableMouseReporting() {
		return config.mouseReporting;
	}
	
	@Override
	public boolean altSendsEscape() {
		return config.altSendsEscape;
	}
	
	@Override
	public boolean ambiguousCharsAreDoubleWidth() {
		return config.ambiguousDoubleWidth;
	}
	
	public static class Config {
		// 字体设置
		public float fontSize = 16;
		public String fontFamily = "Consolas";
		// 颜色设置
		public TerminalColor
				backgroundColor = new TerminalColor(30, 30, 30),
				textColor = new TerminalColor(200, 200, 200),
				cursorColor = new TerminalColor(240, 240, 240),
				foundTextColor = new TerminalColor(255, 255, 0),
				foundBackgroundColor = new TerminalColor(50, 50, 150),
				linkTextColor = new TerminalColor(255, 105, 180),
				linkBackColor = backgroundColor,
				selectTextColor = new TerminalColor(250, 250, 250),
				selectBackColor = new TerminalColor(30, 144, 255);
		// 调色板 (16 基本色 + 240 RGB 色 + 24 灰度)
		public List<TerminalColor> palette = Collections.emptyList();
		// 行为设置
		public boolean useInverseSelection = false;
		public boolean copyOnSelect = true;
		public boolean pasteOnMiddleClick = true;
		public boolean audibleBell = true;
		public boolean mouseReporting = true;
		public boolean altSendsEscape = true;
		public boolean ambiguousDoubleWidth = false;
		// 性能设置
		public int maxScrollbarLines = 5000;
		// 主题元数据
		public String themeName = "Custom Theme";
	}
	
	// 预设主题创建方法
	public static Theme createDarkTheme() {
		Config config = new Config();
		config.themeName = "Dark Theme";
		config.backgroundColor = new TerminalColor(30, 30, 30);
		config.textColor = new TerminalColor(220, 220, 220);
		config.cursorColor = new TerminalColor(240, 240, 240);
		return new Theme(config);
	}
	
	public static Theme createLightTheme() {
		Config config = new Config();
		config.themeName = "Light Theme";
		config.backgroundColor = new TerminalColor(240, 240, 240);
		config.textColor = new TerminalColor(30, 30, 30);
		config.cursorColor = new TerminalColor(0, 0, 0);
		config.selectBackColor = new TerminalColor(100, 180, 255);
		return new Theme(config);
	}
	
	public static Theme createSolarizedDark() {
		Config config = new Config();
		config.themeName = "Solarized Dark";
		config.backgroundColor = new TerminalColor(0, 43, 54);
		config.textColor = new TerminalColor(131, 148, 150);
		config.cursorColor = new TerminalColor(147, 161, 161);
		return new Theme(config);
	}
}